package com.zzqstart.cateservice.controller.front;

import com.zzqstart.cateservice.entity.vo.RecipeFrontVo;
import com.zzqstart.cateservice.entity.vo.PublishRecipeVo;
import com.zzqstart.cateservice.entity.vo.RecipeQueryVo;
import com.zzqstart.cateservice.service.CateRecipeService;
import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ClassName: RecipeFrontController
 * Package: com.zzqstart.cateservice.controller.front
 * Description:
 *
 * @Author zzqstart
 * @Create 2023-03-07 1:41
 * @Version 1.0
 */
@Api(tags = "前台菜谱数据")
@RestController
@RequestMapping("/cateservice/recipe/front")
public class RecipeFrontController {

    @Autowired
    private CateRecipeService cateRecipeService;

    @ApiOperation(value = "获取菜谱信息")
    @PostMapping("/{page}/{limit}")
    public R getRecipeFrontList(@PathVariable Long page, @PathVariable Long limit,
                                @RequestBody(required = false) RecipeQueryVo recipeQueryVo) {
        Map<String,Object> map = cateRecipeService.getRecipeFrontList(page,limit,recipeQueryVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "发布或更新菜谱")
    @PostMapping("/publishRecipe")
    public R publishRecipe(@RequestBody PublishRecipeVo createRecipeVo, HttpServletRequest request) {
        if (StringUtils.isEmpty(createRecipeVo)){
            return R.error().message("菜谱不能为空");
        }

        String publisherID = JwtUtils.getMemberIdByJwtToken(request);
        createRecipeVo.setPublisherId(publisherID);
        boolean isPublish = cateRecipeService.publishRecipe(createRecipeVo);

        if (isPublish){
            return R.ok().data("publisherID",publisherID);
        }else {
            return R.error().message("发布失败");
        }
    }

}
